package com.wangijun.yuan.admin.system.controller;

import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.crypto.digest.MD5;
import com.wangijun.yuan.admin.base.CommonResult;
import com.wangijun.yuan.admin.base.YuanAdminConst;
import com.wangijun.yuan.admin.system.domain.SysUser;
import com.wangijun.yuan.admin.system.model.em.EnableStatus;
import com.wangijun.yuan.admin.system.model.vo.UserVO;
import com.wangijun.yuan.admin.system.service.IndexService;
import com.wangijun.yuan.admin.system.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 功能描述: 一些无关业务的API
 *
 * @author wlf 2023/9/13 10:30
 */
@Tag(name = "Auth相关接口")
@Slf4j
@RestController
public class IndexController {
    @Resource
    IndexService indexService;
    @Resource
    private SysUserService userService;


    @Schema(description = "登录参数")
    public record LoginParam(
            @NotBlank(message = "参数不可为空")
            String userName,
            @NotBlank(message = "参数不可为空")
            String password) {
    }

    public record TokenVo(String token,
                          String refreshToken) {
    }

    /**
     * 功能描述: 登录操作
     *
     * @author wlf 2023/9/13 10:30
     */
    @Operation(summary = "密码登录", description = "密码登录")
    @PostMapping("/auth/login")
    CommonResult<TokenVo> login(@RequestBody @Validated LoginParam param) {
        // 判断用户是否存在
        SysUser sysUser = userService.lambdaQuery().eq(SysUser::getLoginNo, param.userName).one();
        if (sysUser == null) {
            CommonResult.error("用户名或密码不正确,登陆失败");
        }
        String s = MD5.create().digestHex16(param.password);
        if (!s.equals(sysUser.getPssswd())) {
            CommonResult.error("用户名或密码不正确,登陆失败");
        }
        if (EnableStatus.disable.equals(sysUser.getStatus())) {
            CommonResult.error("用户已失效,登陆失败");
        }
        SaLoginModel loginModel = new SaLoginModel();
        loginModel.setDevice(YuanAdminConst.LoginDevice.PC.name());
        StpUtil.login(sysUser.getId(), loginModel);
        return CommonResult.data(new TokenVo(StpUtil.getTokenValue(), StpUtil.getTokenValue()));
    }

    @Operation(summary = "获取用户信息", description = "获取用户信息")
    @GetMapping("/auth/getUserInfo")
    public CommonResult<Dict> getUserInfo() {
        UserVO userVO = userService.get(StpUtil.getLoginIdAsLong());
        return CommonResult.data(
                Dict.create()
                        .set("userId", userVO.getId())
                        .set("userName", userVO.getName())
                        .set("roles", StpUtil.getPermissionList())
                        .set("buttons", StpUtil.getPermissionList())
        );
    }

    /**
     * 功能描述: 退出登录
     *
     * @author wlf 2023/9/13 10:31
     */
    @GetMapping("/logout")
    CommonResult<Boolean> logout() {
        StpUtil.logout();
        return CommonResult.data(true);
    }


    /**
     * 功能描述: 清理缓存成功
     *
     * @author wlf 2023/9/13 14:06
     */
    @GetMapping("/cleanCache")
    CommonResult<Boolean> cleanCache() {
        indexService.cleanCache();
        return CommonResult.data(true);
    }
}
